home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / stdio / c / filbuf < prev    next >
Text File  |  1996-11-09  |  1KB  |  63 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/stdio/c/RCS/filbuf,v $
  4.  * $Date: 1996/05/06 09:01:34 $
  5.  * $Revision: 1.2 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: filbuf,v $
  10.  * Revision 1.2  1996/05/06 09:01:34  unixlib
  11.  * Updates to sources made by Nick Burrett, Peter Burwood and Simon Callan.
  12.  * Saved for 3.7a release.
  13.  *
  14.  * Revision 1.1  1996/04/19 21:32:42  simon
  15.  * Initial revision
  16.  *
  17.  ***************************************************************************/
  18.  
  19. static const char rcs_id[] = "$Id: filbuf,v 1.2 1996/05/06 09:01:34 unixlib Rel $";
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25.  
  26. __STDIOLIB__
  27.  
  28. int
  29. __filbuf (register FILE * f)
  30. {
  31.   register int n, b, g = f->flag;
  32.   register unsigned char *a;
  33.  
  34.   if ((g & (_IOREAD | _IOERR | _IOEOF)) != _IOREAD)
  35.     return (-1);
  36.  
  37.   b = (g & _IONBF) ? 1 : f->bufsiz;
  38.  
  39.   if (!(a = f->i_base))
  40.     {
  41.       if (!(a = f->i_base = malloc (b + 1)))
  42.     {
  43.       f->flag = g | _IOERR;
  44.       return (-1);
  45.     }
  46.     }
  47.  
  48.   ++a;                /* for ungetc() */
  49.  
  50.   if ((n = read (f->fd, a, b)) <= 0)
  51.     {
  52.       f->flag |= ((n) ? _IOERR : _IOEOF);
  53.       f->i_cnt = 0;
  54.       f->i_ptr = a;
  55.       return (-1);
  56.     }
  57.  
  58.   f->pos += n;
  59.   f->i_cnt = n - 1;
  60.   f->i_ptr = a + 1;
  61.   return (*a);
  62. }
  63.